import Vue from 'vue'
import Router from 'vue-router'
import Parent from 'components/home/home'
import Index from 'particals/index'
import Home from '../App.vue'
import store from 'store';

Vue.use(Router)

export default new Router({
  // mode: 'history',
  routes: [{
    path: '/',
    component: Parent,
    beforeEnter: requireAuth,
    children: [{
      path: '',
      component: resolve => require(['components/content/content'], resolve),
    }]
  },
  {
    path: '/users',
    component: Parent,
    beforeEnter: requireAuth,
    children: [{
      path: '/',
      component: resolve => require(['components/users/users'], resolve),
    },{
      path: ':id/edit',
      component: resolve => require(['components/users/edit'], resolve),
    }],
  },
  {
    path: '/cates',
    component: Parent,
    beforeEnter: requireAuth,
    children: [{
      path: '/',
      component: resolve => require(['components/cates/cates'], resolve),
    },{
      path: 'create',
      component: resolve => require(['components/cates/create'], resolve),
    },{
      path: ':id/edit',
      component: resolve => require(['components/cates/edit'], resolve),
    }],
  },
  {
    path: '/items',
    component: Parent,
    beforeEnter: requireAuth,
    children: [{
      path: '/',
      component: resolve => require(['components/items/items'], resolve),
    },{
      path: 'create',
      component: resolve => require(['components/items/create'], resolve),
    },{
      path: ':id/edit',
      component: resolve => require(['components/items/edit'], resolve),
    }],
  },
  {
    path: '/notes',
    component: Parent,
    beforeEnter: requireAuth,
    children: [{
      path: '/',
      component: resolve => require(['components/notes/notes'], resolve),
    },{
      path: 'create',
      component: resolve => require(['components/notes/create'], resolve),
    },{
      path: ':id/edit',
      component: resolve => require(['components/notes/edit'], resolve),
    }],
  },
  {
    path: '/tags',
    component: Parent,
    beforeEnter: requireAuth,
    children: [{
      path: '/',
      component: resolve => require(['components/tags/tags'], resolve),
    },{
      path: 'create',
      component: resolve => require(['components/tags/create'], resolve),
    },{
      path: ':id/edit',
      component: resolve => require(['components/tags/edit'], resolve),
    }],
  },
  {
    path: '/links',
    component: Parent,
    beforeEnter: requireAuth,
    children: [{
      path: '/',
      component: resolve => require(['components/links/links'], resolve),
    },{
      path: 'create',
      component: resolve => require(['components/links/create'], resolve),
    },{
      path: ':id/edit',
      component: resolve => require(['components/links/edit'], resolve),
    }],
  },
  {
    path: '/comments',
    component: Parent,
    beforeEnter: requireAuth,
    children: [{
      path: '/',
      component: resolve => require(['components/comments/comments'], resolve),
    },{
      path: ':id/edit',
      component: resolve => require(['components/comments/edit'], resolve),
    }],
  },
  {
    path: '/messages',
    component: Parent,
    beforeEnter: requireAuth,
    children: [{
      path: '/',
      component: resolve => require(['components/messages/messages'], resolve),
    },{
      path: ':id/edit',
      component: resolve => require(['components/messages/edit'], resolve),
    }],
  },
  {
    path: '/setting',
    component: Parent,
    beforeEnter: requireAuth,
    children: [{
      path: '/',
      component: resolve => require(['components/setting/setting'], resolve),
    }],
  }]
})

function requireAuth (to, from, next) {
  if(store.state.token.access_token) {
    return next()
  }else{
    window.location.href = "http://www.jindai.com/#/error/404/page-not-find"
  }
}
